package com.wangbo.suzhengtong;

import com.alibaba.fastjson.JSONObject;
import com.wangbo.test.util.JacksonUtil;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.Base64;
import java.util.Map;

public class SZTUtil {
    /**
     * 获取token
     */
    public static String refreshToken(String pubKey, String username, String password, String appId) throws IOException {
        String url = "https://szt.nantong.cn/cityCode/openPlatform/apiapply/refreshToken";
        RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory());

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username", username);
        jsonObject.put("password", SM2Utils.encrypt(Util.hexToByte(pubKey), password.getBytes()));
        jsonObject.put("app_id", appId);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<JSONObject> httpEntity = new HttpEntity<>(jsonObject, headers);
        Map result = restTemplate.postForObject(url, httpEntity, Map.class);
        System.out.println(result);
        Map map = JacksonUtil.toMap((String) result.get("msg"));
        return (String) map.get("token");
    }

    /**
     * 实名认证
     * 传入身份证姓名、号码、校验是否为实名
     */
    public static boolean checkId(String token, String pubKey, String appId, String personalIdNo, String name) throws IOException {
        String url = "https://szt.nantong.cn/cityCode/openPlatform/verify/checkIdCardBase";
        RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory());

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("card_id", personalIdNo);
        jsonObject.put("card_name", name);

        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("app_id", appId);
        param.add("data", SM2Utils.encrypt(Util.hexToByte(pubKey), jsonObject.toString().getBytes()));


        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("op-token", token);
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(param, headers);
        Map result = restTemplate.postForObject(url, httpEntity, Map.class);
        System.out.println(result);
        return result.get("result_code").equals("0");
    }

    /**
     * 实人认证
     * 传入身份证姓名、号码、照片校验是否为本人
     */
    public static boolean checkPhoto(String token, byte[] bytes, String personalIdNo, String name, String appId, String pubKey) throws IOException {
        String url = "https://szt.nantong.cn/cityCode/openPlatform/realpersonauth/checkIdCardFace";
        RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory());

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("card_id", personalIdNo);
        jsonObject.put("card_name", name);
        jsonObject.put("photo_begin", 3);


        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("app_id", appId);
        param.add("data", SM2Utils.encrypt(Util.hexToByte(pubKey), jsonObject.toString().getBytes()));
        param.add("card_photo", "abc" + Base64.getEncoder().encodeToString(bytes));

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("op-token", token);
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(param, headers);
        Map result = restTemplate.postForObject(url, httpEntity, Map.class);
        System.out.println(result);
        return result.get("result_code").equals("0");
    }
}
